<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>品牌列表案例</title>
    <link rel="stylesheet" href="./lib/bootstrap.css">
    <link rel="stylesheet" href="./css/brandlist.css">
</head>

<body>
    <div id="app">
        <div class="card">
            <div class="card-header">
                添加品牌
            </div>
            <div class="card-body">
                <form @submit.prevent="add">
                    <div class="form-row">
                        <div class="col-auto">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <div class="input-group-text">名称</div>
                                </div>
                                <input type="text" class="form-control" placeholder="输入名称" v-model.trim="brand">
                            </div>
                        </div>
                        <div class="col-auto">
                            <button type="submit" class="btn btn-primary mb-2" >添加</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">名称</th>
                    <th scope="col">状态</th>
                    <th scope="col">时间</th>
                    <th scope="col">操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input" :id="'customSwitch' + item.id" v-model="item.status">
                            <label class="custom-control-label" :for="'customSwitch' + item.id" v-if="item.status">已启用</label>
                            <label class="custom-control-label" :for="'customSwitch' + item.id" v-else>已禁用</label>
                        </div>
                    </td>
                    <td>
                        {{item.time}}
                    </td>
                    <td>
                        <a href="javascript:;" @click="remove(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script src="./lib/vue-2.6.12.js"></script>
<script>
    const vm = new Vue({
        el: '#app',
        data: {
            list: [
                { id: 1, name: '宝马', status: true, time: new Date() },
                { id: 2, name: '奔驰', status: false, time: new Date() },
                { id: 3, name: '奥迪', status: true, time: new Date() },
            ],
            brand: '',
            nextId: 4
        },
        methods: {
            remove(id) {
               this.list = this.list.filter(item => item.id !== id)//返回当前id不等的数组
            },
            add(con) {
                if(this.brand === '') {
                    alert('填写名称')
                    return
                }
                const obj = {
                    id: this.nextId,
                    name: this.brand,
                    status: true,
                    time: new Date()
                }
                this.list.push(obj)
                this.brand = ''
                this.nextId++
            }
        }
    })
</script>

</html>